﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


namespace DAL
{
    public class UsersDataModel
    {
        HomestayWebDBEntities ent = new HomestayWebDBEntities();
        public List<User> SelectUser(string username)
        {           
            return ent.Store_User_SelectUserbyUsername(username).ToList();
        }

        public List<User> SelectUserByEmail(string email)
        {
            return ent.Store_User_SelectUserByEmail(email).ToList();
        } 
        public void InsertUser(User user)

        {
            ent.AddToUsers(user);
            ent.SaveChanges();
           
        }
        public bool CheckEmailExist(string email)
        {
            bool exist = false;
            var query = from em in ent.Users
                        where em.Email == email
                        select em;
           
            if(query!=null)
            {
                List<User> list = query.ToList();
                if(list.Count>0)
                {
                    exist= true;//Existed
                }
                
            }
            return exist;
        }
        public List<Store_User_GetAuthorOfPost_Result> GetAuthorOfPost(int postID)
        {
            List<Store_User_GetAuthorOfPost_Result> l = ent.Store_User_GetAuthorOfPost(postID).ToList();
            return l;
        }
        public User UpdateUser(User userobj)
        {
            try
            {
                string containername = ent.DefaultContainerName;
                var keyValues = new KeyValuePair<string, object>[] { new KeyValuePair<string, object>("Username", userobj.Username) };
                var key = new EntityKey(containername + ".Users", keyValues);

                User user = (User)ent.GetObjectByKey(key);
                user.Fullname = userobj.Fullname;
                user.Password = userobj.Password;//new password
                user.Gender = userobj.Gender;
                user.Phone = userobj.Phone;
                user.Email = userobj.Email;
                user.Address = userobj.Address;
                user.AvatarPicture = userobj.AvatarPicture;
                ent.SaveChanges();
                return user;
            }
            catch
            {
                //do nothing
                return null;
            }


        }
        public bool UpdatePassword(string username, string pass)
        {
            try
            {
                string containername = ent.DefaultContainerName;
                var keyValues = new KeyValuePair<string, object>[] { new KeyValuePair<string, object>("Username", username) };
                var key = new EntityKey(containername + ".Users", keyValues);

                User user = (User)ent.GetObjectByKey(key);
                user.Password = pass;//new password
                ent.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                //do nothing
                return false;
            }
        }
        public List<Store_User_GetUserOfSystem_Result> GetListOfUser(int roleID)
        {
            return ent.Store_User_GetUserOfSystem(roleID).ToList();
        }
        //public List<Store_User_GetUserOfSystem_Result> SearchUserInSystem(int roleID, string SearchUsername, int SearchRole, int SearchStatus)
        //{
        //    if (String.Empty.Equals(SearchUsername))
        //    {
        //        return ent.Store_User_SearchUserInSystem(roleID, "", SearchRole, SearchStatus).ToList();
        //    }
        //    else
        //    {
        //        return ent.Store_User_SearchUserInSystem(roleID, SearchUsername, SearchRole, SearchStatus).ToList();
        //    }
        //}
        public Store_User_GetUserOfSystemDetail_Result getUserOfSystemDetail(string username)
        {
            List<Store_User_GetUserOfSystemDetail_Result> list = ent.Store_User_GetUserOfSystemDetail(username).ToList();
            if (list.Count > 0)
            {
                return list[0];
            }
            return null;
        }
        public int BanUser(string username)
        {
             return ent.Store_User_BanUser(username);
                
        }
    }

  
}
